<?php
// The Routing Engine Class can be used to quickly leverage content based on 
// request strings derived from the url or inline function arguments.
// 
// The class assumes all routes follow the form www.site.com/[dir if needed]/$page_id
class RoutingEngine {
  
  private $_RTYPE; #Internal copy of route engine type
  private $_ROUTES; #Collection of internal aliases
  private $_PID; #Internal copy of global $page_id
  
  //These should probably become constants.
  public static $DB_ONLY  = 1;
  public static $FS_ONLY  = 2;
  public static $DB_FIRST = 3;
  public static $FS_FIRST = 4;
  
  #DB_ONLY -> Only checks the database before 404ing
  #FS_ONLY -> Only checks the filesystem before 404ing
  #DB_FIRST -> Checks everywhere, but prefering the DB to FS
  #FS_FIRST -> Chesks everywhere, but prefering the FS to DB
  
  function __construct( $rtype = null) {
    switch ( RoutingType ) {
     case "DB_ONLY" : $this->_RTYPE =  self::$DB_ONLY;  break;
     case "FS_ONLY" : $this->_RTYPE =  self::$FS_ONLY;  break;
     case "DB_FIRST": $this->_RTYPE = self::$DB_FIRST;  break;
     case "FS_FIRST": $this->_RTYPE = self::$FS_FIRST;  break;
             default: $this->_RTYPE =  self::$DB_ONLY;  break;
    } //neat und clean, neat und clean
    $this->_ROUTES = Array();
    $this->_PID = DefaultPath;
  }
  
  function addAlias( $requestString, $targetPath ) {
    $this->_ROUTES[$requestString] = $targetPath;
  }
  
  function getRouteData( $requestString = null) {
    if ( !isset( $requestString ) ) {
      if ( isset($GLOBALS["page_id"] ) ) {
        $this->_processRequestURL();
        $requestString = $GLOBALS["page_id"];
      }       
      else $requestString = $this->_PID;
    }
    $r = "";
    if ( $this->_RTYPE == self::$DB_ONLY  ) {
      $r = $this->_tryDB( $requestString );
      if ( $r != "" ) return $r;
    }
    else if ( $this->_RTYPE == self::$FS_ONLY ) {
    $r = $this->_tryFS( $requestString );
      if ( $r != "" ) return $r;
    }
    else if ( $this->_RTYPE == self::$DB_FIRST ) {
      $r = $this->_tryDB( $requestString );
      if ( $r != "" ) return $r;
      $r = $this->_tryFS( $requestString );
      if ( $r != "" ) return $r;
    }
    else if ( $this->_RTYPE == self::$FS_FIRST ) {
      $r = $this->_tryFS( $requestString );
      if ( $r != "" ) return $r;
      $r = $this->_tryDB( $requestString );
      if ( $r != "" ) return $r;
    }
    else return $this->_fail( $requestString );
    return $this->_fail( $requestString );
  }
  
  private function _tryDB( $rs ) {
    $page = ( isset( $this->_ROUTES[$rs] ) ) ? $this->_ROUTES[$rs] : $rs;
    $query = "SELECT * FROM pages WHERE name = '$page'";
    $pagevals_arr = Array();
    $r = "";
    try {
      $dbh = new PDO( "mysql:host=" . DB_Hostname . ";dbname="
                      . DB_Name, DB_Username, DB_Password );
      foreach ($dbh->query($query) as $row)
      {
        $pagevals_arr[] = $row;
      }
      $dbh = null;
      if ( count($pagevals_arr) > 0 ){
        #parse out the values
        foreach ( $pagevals_arr as $value ) $r .= "<br />" . $value["text"];
      } else return "";
      return $r;
    } catch(PDOException $e){ return ""; }  
  }
  
  private function _tryFS( $rs ) {
    $page = ( isset( $this->_ROUTES[$rs] ) ) ? $this->_ROUTES[$rs] : $rs;
    if ( file_exists( "pages/$page.php" ) )
      return file_get_contents( "pages/$page.php" );
    else return ""; 
  }
  
  private function _fail( $rs ) {
    if ( file_exists( "pages/404.php" ) )
      return file_get_contents( "pages/404.php?pageid=$rs" );
    else return "Oh dear, something really bad has happened. This is a 404 error for a 404 error page."; 
  }
  

  function _processRequestURL(){
    global $page_id;
    $args = explode("/", $_SERVER["REQUEST_URI"]);
    $lvl = ( DirectoryLevel < 1 ) ? 1 : DirectoryLevel;
    $page_id = strtolower(($args[$lvl] == "") ?
                                       "home" :
                                 $args[$lvl]) ;
  }

} ?>